/*
The MIT License (MIT)

Copyright (c) 2013 Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdIGame.h"

namespace hhd
{
    static LRESULT CALLBACK handleWindowsMessage(IGame& in_game,
            Vector<WNDPROC> const& in_manager_callbacks,
            HWND p_window_handle,
            UINT p_message,
            WPARAM p_word,
            LPARAM p_long_word)
    {
        switch (p_message)
        {
            // Allow the user to press the escape key to end the application
        case WM_KEYDOWN	:
            switch(p_word)
            {
                // Check if the user hit the escape key
            case VK_ESCAPE :
                in_game.exit();
                break;
            }
            break;

        case WM_SIZE:
            switch(p_word)
            {
            case SIZE_MAXHIDE:
            case SIZE_MINIMIZED:
                in_game.pauseSystem();
                break;
            }
            break;

        case WM_ENTERSIZEMOVE:
            in_game.pauseSystem();
            break;

        case WM_EXITSIZEMOVE:
            in_game.unPauseSystem();
            break;

            //check for activity, use this to pause play, may have effects on editor
        case WM_ACTIVATE :
            switch(p_word)
            {
            case WA_INACTIVE :
                in_game.pauseSystem();
                break;

            case WA_ACTIVE :
            case WA_CLICKACTIVE :
                in_game.unPauseSystem();
                break;
            }
            break;

            ////check for the space bar to switch to full screen
            //case WM_CHAR :
            //	switch(p_word)
            //	{
            //		case ' ' :
            //			break;
            //	}
            //	return 0;


            // The user hit the close button, close the application
            //case WM_DESTROY	:
        case WM_CLOSE:
            in_game.exit();
            break;
        }

        //check to see if any manager callback functions care about this message
        LRESULT long_results = 0;

        for(size_t i = 0; i < in_manager_callbacks.size(); i++)
        {
            long_results |= in_manager_callbacks[i](p_window_handle, p_message, p_word, p_long_word);
        }

        //if they had an issue return it
        if(long_results)
        {
            return long_results;
        }
        //otherwise
        else
        {
            return DefWindowProc(p_window_handle, p_message, p_word, p_long_word);
        }
    }

    static LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        LONG_PTR user_data = GetWindowLongPtr(hWnd, GWLP_USERDATA);
        IGame* game = reinterpret_cast<IGame*>( user_data );

        if(game)
        {
            Vector<WNDPROC> const& callbacks = (Vector<WNDPROC> const&)game->getManagerCallbacks();

            return handleWindowsMessage(*game,
                                        callbacks,
                                        hWnd,
                                        message,
                                        wParam,
                                        lParam);
        }
        else
        {
            return ::DefWindowProc(hWnd, message, wParam, lParam);
        }
    }

    void* IGame::getOsMessageConsumeFunction()
    {
        return &wndProc;
    }

    void IGame::updateOS()
    {
        ::MSG msg = {0};

		do
        {
            if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
            else
            {
                break;
            }
		} while(msg.message != WM_QUIT);
    }
}